package com.podcstr.service;

import com.podcstr.dao.MediaFileDao;
import com.podcstr.dao.UserDao;
import com.podcstr.dao.UserProfileDao;
import com.podcstr.entity.MediaFile;
import com.podcstr.entity.User;
import com.podcstr.entity.UserProfile;
import com.podcstr.service.exception.ServiceException;
import org.apache.log4j.Logger;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author azim
 * @since 5/12/13 
 */

@Stateless
@Local(UserService.class)
public class UserServiceImpl implements UserService {

    private final Logger log = Logger.getLogger(UserServiceImpl.class.getName());

    @Inject
    private UserDao userDao;

    @Inject
    private UserProfileDao userProfileDao;

    @Inject
    private MediaFileDao mediaFileDao;

    /*@Inject
    private ICryptString iCryptString;*/

    @Override
    public void signUp(User user) throws ServiceException {

        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set violations = validator.validate(user);
        if (violations.size() > 0) {

            log.error(violations);
            throw new ServiceException(violations);
        }

        User existingUsers = userDao.findUserByUsername(user.getUsername());
        if (existingUsers != null) {

            throw new ServiceException("Duplicate User. This user already exists.");
        }

        try {

            MediaFile mediaFile = new MediaFile();
            mediaFile.setContentType("");
            mediaFile.setName("");
            mediaFile.setContent(new byte[10]);
            mediaFileDao.add(mediaFile);
            UserProfile userProfile = new UserProfile();
            userProfile.setPhoto(mediaFile);
            userProfileDao.add(userProfile);
            user.setUserProfile(userProfile);
            userDao.add(user);
            log.info(new Date() + " : Signed up a new user with Username : " + user.getUsername());
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    }

    @Override
    public List<User> findAll() {

        log.info(new Date() + " : Returning the list of users.");
        return userDao.findAll();
    }

    @Override
    public void delete(long userId) {

        userDao.delete(userId);
        log.info(new Date() + " : Deleting an user with id : " + userId + ".");
    }

    @Override
    public void toggleDisable(long userId) {

        userDao.toggleDisable(userId);
        log.info(new Date() + " : Toggling user with id : " + userId + " active value.");
    }

    @Override
    public void update(User user) {

        userDao.update(user);
        log.info(new Date() + " : Updated the user with id : " + user.getId() + ".");
    }

    @Override
    public void updateProfile(UserProfile userProfile) {

        userProfileDao.update(userProfile);
        log.info(new Date() + " : Updated the user profile with id : " + userProfile.getId() + ".");
    }

    @Override
    public void updateFile(MediaFile mediaFile) {

        mediaFileDao.update(mediaFile);
        log.info(new Date() + " : Updated the user profile's mediaFile with id : " + mediaFile.getId() + ".");
    }

    @Override
    public User findUserByUsername(String username) {

        log.info(new Date() + " Returning the user with username : " + username + ".");
        return userDao.findUserByUsername(username);
    }

    @Override
    public boolean verifyLogin(String username, String password) {

        User user = userDao.findUserByUsername(username);
        log.info(new Date() + " : Verifying login for username : " + username + ".");
        return ((user != null) && user.getPassword().equals(password));
    }
}
